const findMenu = function (menus, path) {
  if (!menus || !(menus instanceof Array)) return null;
  for (let menu of menus) {
    if (menu.path === path) {
      return menu;
    }
    if (menu.children && menu.children instanceof Array && menu.children.length) {
      let r = findMenu(menu.children, path);
      if (r) {
        return r;
      }
    }
  }
  return null;
}

import router from '../router/index'

export default {
  namespaced: true,
  state: {
    menus: [
      {
        id: 1,
        name: '用户管理',
        path: '/user',
        icon: 'el-icon-s-fold',
        children: null
      },
      {
        id: 2,
        name: '权限管理',
        path: '',
        icon: 'el-icon-s-fold',
        children: [
          {
            id: 3,
            name: '角色管理',
            path: '/role',
            icon: 'el-icon-s-fold'
          }
        ]
      },
    ],
    opened: [], // 已打开的功能列表
    menuActive: '', // 左侧菜单当前选中项
    tabBarFloatMenuVisible: false,
  },
  mutations: {
    setMenuActive: (state, path) => {
      state.menuActive = path
    },
    addOpened: (state, menu) => {
      if (menu) {
        state.opened.push(menu);
      }
    },
    deleteOpened: (state, i) => {
      state.opened.splice(i, 1);
    },
    closeTabBarFloatMenu: state => {
      state.tabBarFloatMenuVisible = false;
    },
    openTabBarFloatMenu: state => {
      state.tabBarFloatMenuVisible = true;
    }
  },
  actions: {
    menuSelect: (context, path) => {
      context.commit('setMenuActive', path);
      if (path === '/') {
        return;
      }
      let find = false;
      for (let menu of context.state.opened) {
        if (menu.path === path) {
          find = true;
          break;
        }
      }
      if (!find) {
        let m = findMenu(context.state.menus, path);
        if (m) {
          context.commit('addOpened', m);
        }
      }
    },
    switchSideBarMenu: ({state}, menu) => {
      if (!menu) return;
      if (menu.path === state.menuActive) {
        return;
      }
      router.push({path: menu.path})
    },
    closeTabMenu: ({commit, state,}, menu) => {
      let closeIndex = -1;
      for (let i = 0; i < state.opened.length; i++) {
        let open = state.opened[i];
        if (open.path === menu.path) {
          closeIndex = i;
          break;
        }
      }
      let closeMenu = state.opened[closeIndex];
      commit('deleteOpened', closeIndex);
      if (closeMenu.path !== state.menuActive) {
        // TODO 清除缓存的组件
        return;
      }
      if (state.opened.length > 0) { // 还有打开的菜单
        if (closeIndex === 0) { // 判断关闭的是第一个标签
          router.push({path: state.opened[closeIndex].path}).then();
        } else { // 关闭的不是第一个
          if (closeIndex === state.opened.length) { // 如果是最后一个
            closeIndex--;
            router.push({path: state.opened[closeIndex].path}).then();
          } else { // 如果是中间的（不是第一个也不是最后一个）
            router.push({path: state.opened[closeIndex].path}).then();
          }
        }
      } else { // 没有打开的菜单了
        router.push({path: '/'}).then(); // 否则跳转首页
      }
    }
  },
  getters: {
    getActiveMenu: state => {
      let menu = null;
      for (let m of state.opened) {
        if (m.path === state.menuActive) {
          menu = m;
          break;
        }
      }
      return menu ? menu : {};
    }
  }
}